package binary_tree.LeetCode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * 借助栈完成前序遍历的迭代写法
 */
public class Num144_LevelOrderTranversal {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ret=new ArrayList<>();
        if (root==null){
            return ret;
        }
        Deque<TreeNode> stack=new LinkedList<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode r1=stack.poll();
            ret.add(r1.val);
            if (r1.right!=null){
                stack.push(r1.right);
            }
            if (r1.left!=null){
                stack.push(r1.left);
            }
        }
        return ret;
    }
}
